//
//  LBMWelcomeViewController.swift
//  微博
//
//  Created by 刘保满 on 16/5/23.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class LBMWelcomeViewController: UIViewController {
    

    override func loadView() {
        //直接设置背景视图为当前的view
        view = backView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
 
    }
    
    //把动画放到这个方法中
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //设置动画
    private func startAnimation() {
        
        //移动的距离屏幕的高 - 160,呈现对称
        let offset = -(ScreenHeight - 160)
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: {
            //修改头像的约束实现动画
            //更新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
        
                make.bottom.equalTo(offset)
                
            })
            //刷新父视图
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            //不让昵称和头像一块出来
            UIView.animateWithDuration(0.4, animations: {
                
                self.nameLabel.alpha = 1.0
                
                }, completion: { (_) in
            
                    //MARK:头像和文字都显示,欢迎界面完成,可以发送通知,切换根视图
//                   UIApplication.sharedApplication().keyWindow?.rootViewController = LBMMainViewController()
                    
                    //发送通知
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
                    
                    
                    
                    
            })
 
        }
        
    }
 
    //设置UI界面
    private func setupUI() {
        //添加子控件
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        //设置头像约束
        iconView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            //约束宽高,解决SDWebImage中出现的自动缩放的bug,会出现在6s 和6sp 中圆角bug
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        //设置昵称约束
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        //通过第三方框架,设置头像
        iconView.sd_setImageWithURL(LBMUserAccountViewModel.sharedAccountViewModel.headURL)
        
        
        
        //头像切圆角
        iconView.layer.cornerRadius = 45
        
        nameLabel.alpha = 0
        
        

    }

    //懒加载子视图
    //背景视图
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //昵称
    private lazy var nameLabel: UILabel = UILabel(title: LBMUserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
    
    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    
    
}
